//
//  LHTabBarViewController.swift
//  sinaWeibo
//
//  Created by LiHong on 15/10/26.
//  Copyright © 2015年 LiHong. All rights reserved.
//

import UIKit

class LHTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
  
    //    tabBar.tintColor = UIColor.orangeColor()
        // 首页
        let homeVc = LHHomeTableViewController()
        addChildViewController(homeVc, title: "首页", imageName: "tabbar_home")

        // 消息
        let msgVc = LHMessageTableViewController()
        addChildViewController(msgVc, title: "消息", imageName: "tabbar_message_center")
        
        // 撰写按钮
        let view = UIViewController()
        addChildViewController(view)
        
        // 发现
        let discoverVc = LHDiscoverTableViewController()
        addChildViewController(discoverVc, title: "发现", imageName: "tabbar_discover")
        
        // 我
        let profileVc = LHProfileTableViewController()
        addChildViewController(profileVc, title: "我", imageName: "tabbar_profile")
        
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        let width = tabBar.bounds.width / CGFloat(5)
        composeButton.frame = CGRect(x: width * 2, y: 0, width: width, height: tabBar.bounds.height)
        tabBar.addSubview(composeButton)
    }
    /**
     添加子空间
     
     - parameter childController: 子控制器
     - parameter title:           子标题
     - parameter imageName:       图片名称
     */
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        let controller = childController
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
//        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
//        UIFontDescriptorNameAttribute
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    
    
    
    ///撰写按钮
    lazy var composeButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    func composeButtonClick() {
        let vc = LHUserAccount.userLogin() ? LHComposeViewController() : LHOAuthViewController()
        
        presentViewController(UINavigationController(rootViewController: vc), animated: true, completion: nil)
        
    }
}
